package com.fuyd.pms.entity.base;

import com.fuyd.pms.utils.Identities;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.id.IdentityGenerator;

import java.io.Serializable;

/**
 * genId生成策略
 * Created by fuyongde on 2015/12/25.
 */
public class GenGenerator extends IdentityGenerator {

    public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
        if (object == null) throw new HibernateException(new NullPointerException());

        Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);

        if (id == null) {
            //id is null it means generate ID
            //id = super.generate(session, obj);
            id = Identities.genId();
            return id;
        } else {
            //id is not null so using assigned id.
            return id;
        }

    }
}
